<template>
  <div class="text-preview">
    <div class="text-container" v-loading="loading">
        <div v-if="textContent" class="text-content">
          <pre class="preview-text">{{ textContent }}</pre>
        </div>
      
      <div v-else-if="error" class="error-message">
        <el-icon><Document /></el-icon>
        <p>文本文件加载失败</p>
      </div>
    </div>
    
  </div>
</template>

<script setup>
import { ref, onMounted, computed } from 'vue'
import { Document, CopyDocument, Download } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'

const props = defineProps({
  fileInfo: {
    type: Object,
    default: () => ({})
  }
})

const loading = ref(true)
const error = ref(false)
const textContent = ref('')

// 加载文本内容
const loadTextContent = async () => {
  try {
    loading.value = true
    error.value = false
    
    const response = await fetch(props.fileInfo.fileUrl)
    if (response.ok) {
      textContent.value = await response.text()
    } else {
      error.value = true
    }
  } catch (err) {
    console.error('文本内容加载失败:', err)
    error.value = true
  } finally {
    loading.value = false
  }
}


onMounted(() => {
  if (props.fileInfo.fileUrl) {
    loadTextContent()
  }
})
</script>

<style scoped>
.text-preview {
  display: flex;
  flex-direction: column;
  height: 100%;
}

.text-container {
  flex: 1;
  display: flex;
  background: #f5f5f5;
  border-radius: 8px;
  min-height: 400px;
  position: relative;
}

.text-content {
  width: 100%;
  padding: 16px;
  overflow: auto;
}

.preview-text {
  font-family: 'Courier New', monospace;
  font-size: 14px;
  line-height: 1.6;
  color: #333;
  white-space: pre-wrap;
  word-wrap: break-word;
  margin: 0;
  background: white;
  padding: 16px;
  border-radius: 4px;
  border: 1px solid #e0e0e0;
}

.error-message {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: #999;
  font-size: 14px;
  width: 100%;
}

.error-message .el-icon {
  font-size: 48px;
  margin-bottom: 12px;
}

</style>
